<template>
  <div class="product__container">
    <dialogForm></dialogForm>
    <div class="search">
      <el-col :span="10">
        <el-autocomplete class="inline-input"
          v-model="searchName"
          :fetch-suggestions="querySearch"
          placeholder="请输入查询姓名"
          :trigger-on-focus="false"
          @select="handleSelect"
          :clearable="true"
          @keydown.enter.native="searchList(searchName)"
          @input="inputHandler"></el-autocomplete>
      </el-col>
      <el-button type="primary"
        icon="el-icon-search" @click="searchList(searchName)"></el-button>
      <el-button type="primary"
        class="el-button-add" @click="$store.state.dialogFormVisible=true"
        v-has="'add'">添加项目</el-button>
    </div>
    <div class="selectClass">
      <span>选择班级</span>
      <el-select v-model="value"
        placeholder="请选择"
        @change="changeClass">
        <el-option v-for="item in classList"
          :key="item.label"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </div>
    <div class="exportExcel">
      <el-button @click="exportExcel">导出excel数据</el-button>
    </div>
    <el-table :data="tableData"
      border
      style="width: 100%">
      <el-table-column label="头像"
        width="100">
        <template slot-scope="scope">
          <span >
            <img :src="scope.row.head" alt="" width="50" height="50">
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="name"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column prop="class"
        label="班级">
      </el-table-column>
      <el-table-column prop="productUrl"
        label="项目">
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{row}">
          <el-button type="primary"
            icon="el-icon-view"
            size="mini"
            class="chakan"
            v-has="'read'"
            @click="chakan(row)">查看</el-button>
          <el-button type="primary"
            icon="el-icon-edit"
            size="mini"
            class="bianji"
            @click="editStudent(row)"
            v-has="'edit'">编辑</el-button>
          <el-button type="primary"
            icon="el-icon-delete"
            size="mini"
            class="shanchu" @click="delStudent(row)"
            v-has="'delete'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pages">
      <el-pagination background
        layout="prev, pager, next"
        :total="dataLength"
        @current-change="currentPage"
        :page-size=pageSize>
      </el-pagination>
    </div>
  </div>
</template>

<script>
import * as studentApi from "../../../../api/student";
import dialogForm from "../../../../components/dialogForm"
import {mapState} from "vuex"
import store from "../../../../store";
import qee from "qf-export-excel"

export default {
  methods: {

    changeClass(e) {//选择班级使selectClass重新赋值

      store.dispatch("CHANGECLASS",e)
      // if (e !== "全部") {
      //   this.selectClass = this.dataList.filter((item) => {
      //     return item.class === e;
      //   });
      // } else {
      //   this.selectClass = this.dataList;
      // }
      // this.dataLength = this.selectClass.length;
      // this.getDataList(1);
      // console.log(this.selectClass);
    },
    getData() {
      // studentApi.getStudentList().then((res) => {
      //   console.log(res.data.data);
      //   let data = res.data.data;
      //   // this.selectClass = data;
      //   store.commit('SET_SELECTLIST', data);
      //   // this.dataList = data;
      //   store.commit('SET_DATALIST',data)
      //   // this.dataLength = data.length;
      //   store.commit('SET_DATALENGTH',data.length)
      //   // console.log(this.dataList)
      //   this.dataList.forEach((item) => {
      //     this.nameList.push({value: item.name });
      //     this.classList.push(item.class);
      //   });
      //   let CLASSLIST = Array.from(new Set(this.classList)).map((item, index) => {
      //       return { value: item, lable: index + 1 };
      //     });
      //   CLASSLIST.unshift({ value: "全部", lable: 0 });
      //   store.commit('SET_CLASSLIST', this.classList);
      //   store.commit('SET_NAMELIST', this.nameList);
      //   console.log(this.classList)
      //   this.getDataList(1);
      // });
    },
    getDataList(page) {
      
      // store.commit("SET_TABLEDATA",[])
      // let TABLEDATA=[]
      // for (
      //   var i = (page - 1) * this.pageSize;
      //   i < this.dataLength && i < page * this.pageSize;
      //   i++
      // ) {
      //   TABLEDATA.push({
      //     head: `<img class="head" src="${this.selectList[i].headimgurl}"/ >`,
      //     name: this.selectList[i].name,
      //     class: this.selectList[i].class,
      //     product: this.selectList[i].productUrl,
      //   });
      // }
      // store.commit("SET_TABLEDATA",TABLEDATA)
      // console.log(this.selectList)
      // console.log(store.state.tableData)
    },
    currentPage(e) {
      store.dispatch('GET_DATALIST',e);
    },
    querySearch(queryString, cb) {
      var nameList = this.nameList;
      var results = queryString
        ? nameList.filter(this.createFilter(queryString))
        : nameList;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        );
      };
    },

    handleSelect(e) {//选中名字触发查询姓名
      // this.selectClass = this.selectClass.filter((item) => {
      //   return item.name===e.value;
      // });
      // this.dataLength = this.selectClass.length;
      // this.getDataList(1);
      store.dispatch("NAMESELECT",e)
    },
    searchList(text){//点击搜索或回车触发查询姓名
      // this.selectClass = this.dataList.filter((item) => {
      //   return item.name.includes(text);
      // });
      // this.dataLength = this.selectClass.length;
      // this.getDataList(1);
      store.dispatch("NAMESEARCH",text)
    },
    
    inputHandler(e){//当搜索栏为空时将所有数据渲染
      if(e===""){
        store.dispatch("NAMESEARCH",e)
      }
    },
    delStudent(row){//点击删除学员信息
      // studentApi.delStudent(row.sId)
       this.$confirm('确认要删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          studentApi.delStudent(row.sId)
          .then(res=>{
            store.dispatch('GET_DATA');
          })
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });   
        });
    },
    editStudent(row){//编辑学员信息
      this.$store.state.dialogFormVisible=true
      this.$store.state.dialogTitle="编辑学员信息"
      store.commit("SET_CURRENTROW",row)
      store.commit("SET_UPDATASID",row.sId)
      console.log(store.state.updataSID)
      console.log(row)
    },
    exportExcel(){
      let titleList=[
        {
        title:"头像地址",
        key:"head",
        },
        {
        title:"姓名",
        key:"name",
        },
        {
        title:"班级",
        key:"class",
        },
        {
        title:"项目",
        key:"productUrl",
        },
      ]

      qee(titleList,this.tableData,"2107")
    },
    chakan(row){
      console.log(row)
      localStorage.setItem("info",JSON.stringify(row))
      this.$router.push("/studentManager/profile")
    }
  },
  mounted() {
    store.dispatch('GET_DATA');
  },
  data() {
    return {
      input: "",
      value: "",
      searchName: "",
    };
  },
  computed:{
    ...mapState(['dataList','selectList','tableData','classList','nameList','dataLength','pageSize','currentRow'])
  },
  components:{
    dialogForm
  }
};
</script>


<style scoped lang="less">
.exportExcel{
  margin-top:20px;
}

.text {
  span {
    font-weight: 600;
  }
}
/deep/.el-input {
  width: 300px;
}
.el-button {
  margin-left: 5px;
  background-color: #ecf5ff;
  color: #409eff;
}
.el-button-add {
  background-color: #409eff;
  color: #fff;
}
.selectClass {
  margin-top: 40px;
  /deep/.el-input {
    margin-left: 20px;
    width: 200px;
  }
}
/deep/.el-table .cell {
  text-align: center;
}
.el-table {
  margin-top: 50px;
}
.chakan,
.bianji {
  background-color: #409eff;
  color: #fff;
}
.shanchu {
  background: red;
  color: #fff;
  border-color: red;
}
/deep/.head {
  height: 50px;
  width: 50px;
  border-radius: 5px;
}
.pages {
  text-align: center;
}
.search {
  width: 720px;
}
</style>